package com.dc.controller;

import com.dc.annotation.Log;
import com.dc.common.ResponseResultVO;
import com.dc.service.FileService;
import com.dc.task.WebsocketClient;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.java_websocket.client.WebSocketClient;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

@Api(tags = "文件上传下载管理")
@RestController
@RequestMapping("/uploadAndDownload")
public class FileController {

    @Resource
    FileService FileService;

    @Resource
    WebsocketClient websocketClient;

    @ApiOperation(value = "test", notes = "test")
    @GetMapping("/test")
    public void test(String url,Integer type) {
        //连接成功,发送信息
        WebSocketClient test = websocketClient.initFactory(url, type);
        test.send("测试发送消息");
    }


    @Log(title = "文件管理", businessDesc = "文件上传")
    @ApiOperation(value = "文件上传功能", notes = "文件上传功能")
    @PostMapping("/uploadFile")
    public ResponseResultVO uploadFile(@RequestParam("files") MultipartFile[] files) {
        return FileService.uploadFile(files);
    }

    @Log(title = "文件管理", businessDesc = "文件下载")
    @ApiOperation(value = "文件下载功能", notes = "文件下载功能")
    @GetMapping("/downloadFile")
    public void downloadFile(@RequestParam String path, @RequestParam String name, HttpServletResponse servletResponse) {
        FileService.downloadFile(path, name, servletResponse);
    }

    @Log(title = "文件管理", businessDesc = "文件删除")
    @ApiOperation(value = "文件删除功能", notes = "文件删除功能")
    @GetMapping("/deleteFile")
    public ResponseResultVO deleteFile(@RequestParam String path) {
        return FileService.deleteFile(path);
    }

}
